package com.tg.app.activity.device.ui.cameraview;
import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tg.app.R;
import com.tg.app.activity.device.CameraViewActivity;
import com.tange.module.device.feature.DeviceFeature;
import com.tg.app.helper.DeviceHelper;
import com.tg.appcommon.android.PermissionUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.business.ICarModule;
import com.tg.appcommon.car.IMapView;
import com.tg.appcommon.router.TGBusiness;
import com.tg.data.bean.DeviceItem;

public class CarLiveFragment extends CameraLiveViewFragment {
    private static final String TAG = "CarLiveFragment";
    private IMapView mTGMapView;
    private View  mMapCardView,mMicPhoneLayout;
    public CarLiveFragment() {
    }

    public static CarLiveFragment newInstance(DeviceFeature deviceFeature, DeviceItem deviceItem) {
        CarLiveFragment fragment = new CarLiveFragment();
        fragment.setArguments(newBundle(deviceFeature,deviceItem));
        return fragment;
    }

    @Override
    public void setSendPTZCmd(boolean isSend) {

    }

    @Override
    public void onStart(){
        super.onStart();
        if (mTGMapView != null){
            mTGMapView.start();
        }
    }


    @Override
    public void onStop(){
        super.onStop();
        if (mTGMapView != null){
            mTGMapView.stop();
        }
    }



    @Override
    public int getPtzControlViewVisible() {
        return View.GONE;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_car_live, container, false);
        layout = view.findViewById(R.id.cameraview);
        FrameLayout frameLayout = view.findViewById(R.id.fl_map_container);
        mTGMapView = TGBusiness.getCarModule().getMapView(getContext(), ICarModule.MAP_FROM_CAR);
        if (mTGMapView != null){
            frameLayout.addView(mTGMapView);
        }else{
            frameLayout.setVisibility(View.GONE);
        }
        init(savedInstanceState);
        this.relMicPhone = view.findViewById(R.id.rel_camera_live_micphone);
        btnSpeaking = view.findViewById(R.id.btn_camera_live_speaking);
        setBtnSpeakingOnTouchListener();
        recordViewIcon = view.findViewById(R.id.record_view_icon_inside);
        recordViewShadowLayout = view.findViewById(R.id.yc_shadow_layout);
        btnSpeaking.setRecordAudioListener(this);
        btnSpeakingLand = view.findViewById(R.id.btn_camera_live_speaking_land);
        btnSpeakingLand.setRecordAudioListener(this);
        lightView = view.findViewById(R.id.camera_live_light_view);
        lightView.setStyle(Paint.Style.FILL);
        lightView.setColor(getContext().getResources().getColor(R.color.breathing_light_color));
        lightView.setParentView(btnSpeaking);
        mMapCardView = view.findViewById(R.id.rel_map_cardView);
        mMicPhoneLayout = view.findViewById(R.id.camera_live_speaking_land_layout);
        mPlayerResolutionLayout = view.findViewById(R.id.camera_live_player_resolution_land_layout);
        setClicked(false);
        return  view;
    }


    @Override
    public void onRecordStop() {
        super.onRecordStop();
        if (recordViewIcon != null) {
            recordViewIcon.setImageResource(R.drawable.ic_tange_global_icon_microphone_gray);
        }
        if (recordViewShadowLayout != null) {
            recordViewShadowLayout.setShadowColor(getResources().getColor(R.color.white_rounded_btn_shadow_color));
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mTGMapView != null) {
            mTGMapView.setDeviceItem(mDeviceItem);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mTGMapView != null){
            mTGMapView.setAllGesturesEnabled(true);
            TGLog.i(TAG, "onResume ");
            mTGMapView.getDriveMoveInfo(false, mDeviceItem.uuid);
        }

        hideMic();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mTGMapView != null){
            mTGMapView.pause();
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mTGMapView != null){
            mTGMapView.saveInstanceState(outState);
        }
    }

    @Override
    protected void configurationView(int orientation) {
        super.configurationView(orientation);
        if (isLandscape){
            mMapCardView.setVisibility(View.GONE);
            btnSpeaking.setVisibility(View.GONE);
            mMicPhoneLayout.setVisibility(View.VISIBLE);

        }else {
            mMapCardView.setVisibility(View.VISIBLE);
            btnSpeaking.setVisibility(View.VISIBLE);
            mMicPhoneLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mTGMapView != null){
            mTGMapView.destroy();
        }

    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    }

    public void setClicked(boolean enable) {
        CameraViewActivity cameraViewActivity = (CameraViewActivity)getActivity();
        if (!DeviceHelper.isOnline(mDeviceItem) && cameraViewActivity != null && !cameraViewActivity.isLocalConnect()){
            super.setClicked(false);
        }else{
            super.setClicked(true);
        }
    }
    private void init(Bundle savedInstanceState) {
        if (mTGMapView != null){
            mTGMapView.init(savedInstanceState);
            mTGMapView.setTGMapViewMoveListener(new IMapView.TGMapViewMoveListener() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onSpeed(double speed) {
                }

                @Override
                public boolean onCheckPermissions() {
                    PermissionUtil permissionUtil = new PermissionUtil(getActivity());
                    return permissionUtil.checkPermissions(getActivity());
                }
            });
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] paramArrayOfInt) {
        if (requestCode == PermissionUtil.PERMISSON_REQUESTCODE) {
            if (!PermissionUtil.verifyPermissions(paramArrayOfInt)) {      //没有授权
                PermissionUtil permissionUtil = new PermissionUtil(getContext());
                permissionUtil.showDialog(getResources().getString(R.string.permission_settings_title), getResources().getString(R.string.permission_settings_location));             //显示提示信息
            }else if (mTGMapView != null){
                if (mTGMapView.isLocationSuccess()){
                    mTGMapView.onClickPhoneLocation();
                }else{
                    mTGMapView.setLocationSuccessPhone(true);
                }
            }
        }
    }

}